// mysql/index.js
var mysql = require("mysql");
const schedule = require("node-schedule"); //定时模块
var config = require("./config.js");
var pool = mysql.createPool({
  host: config.database.HOST,
  user: config.database.USERNAME,
  password: config.database.PASSWORD,
  database: config.database.DATABASE,
});
// let orderId = Math.random().toString().substr(3, 4) + Date.now();
// 生成[100000,999999]六位数随机取货码
// let pickup_code = Math.floor(Math.random() * (999999 - 100000 + 1) + 100000);
// console.log(pickup_code);
class Mysql {
  constructor() {}
  query(MysqlQuery) {
    const sql = MysqlQuery;
    return new Promise((resolve, reject) => {
      pool.getConnection((err, connection) => {
        if (err) {
          reject(err);
          throw error;
        } else {
          connection.query(sql, function (error, results, fields) {
            if (error) {
              throw error;
            }
            if (results) {
              resolve(results);
            }
            connection.release(); //官方写法
          });
        }
      });
    });
  }
  te(dd) {
    console.log(dd);
  }
  // 查询整个表
  select(tableName) {
    const sql = `select * from ${tableName}`;
    return new Promise((resolve, reject) => {
      pool.getConnection((err, connection) => {
        if (err) {
          reject(err);
          throw error;
        } else {
          connection.query(sql, function (error, results, fields) {
            if (error) {
              // reject(fields);
              throw error;
            }
            if (results) {
              resolve(results);
            }
            connection.release(); //官方写法释放连接
            // connection.end();//可以
            // console.log("1");
          });
          // console.log("2");
          // 输出顺序2,1
          // connection.end(); //错误写法，位置不对
          // pool.releaseConnection(connection); //可以
          // connection.release();//可以
        }
      });
    });
  }
  // 删除整个表
  delete(tableName) {
    const sql = `truncate table ${tableName}`;
    this.query(sql);
  }
  // 查询根据openid信息
  allPeopleTest(openid) {
    const sql = `select * from all_people where openid=${openid}`;
    return new Promise((resolve, reject) => {
      pool.getConnection((err, connection) => {
        if (err) {
          reject(err);
          throw error;
        } else {
          connection.query(sql, function (error, results, fields) {
            if (error) {
              throw error;
            }
            if (results) {
              resolve(results);
            }
            connection.release(); //官方写法
          });
        }
      });
    });
  }
  //温度上报每日定时任务
  async temReportJob() {
    console.log("777");
    this.delete("tem_today_report");

    let people_info = await this.select("all_people");
    // console.log(people_info[0].name);
    for (const i in people_info) {
      let name = people_info[i].name;
      let age = people_info[i].age;
      let gender = people_info[i].gender;
      let phone = people_info[i].phone;
      let address = people_info[i].address;
      let openid = people_info[i].openid;
      await this.query(
        `INSERT INTO tem_today_report (openid,name,age,gender,phone,address,tem,time,state) VALUES ('${openid}','${name}', '${age}', '${gender}','${phone}','${address}','-1','-1','-1')`
      );
    }
  }
}
// 每天的凌晨0点0分0秒触发 ：'0 0 0 * * *'
// console.log("0 0 0 * * *");

let timeMysql = new Mysql();
// timeMysql.temReportJob();
// timeMysql.temReportJob();
function scheduleCronstyle() {
  // 每天的凌晨0点0分0秒触发
  schedule.scheduleJob(" 0 0 0 * * * ", () => {
    timeMysql.temReportJob();
    console.log("scheduleCronstyle:" + new Date());
  });
}
scheduleCronstyle();

module.exports = new Mysql();
